package com.oneplatform.tenant.controller;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.jeesuite.common.annotation.ApiMetadata;
import com.jeesuite.common.constants.PermissionLevel;
import com.jeesuite.common.model.IdParam;
import com.jeesuite.common.model.Page;
import com.jeesuite.common.model.PageParams;
import com.jeesuite.common.model.SelectOption;
import com.jeesuite.common.util.BeanUtils;
import com.jeesuite.springweb.model.PageQueryRequest;
import com.oneplatform.tenant.dao.entity.PositionEntity;
import com.oneplatform.tenant.dto.Position;
import com.oneplatform.tenant.dto.param.PositionParam;
import com.oneplatform.tenant.dto.param.PositionQueryParam;
import com.oneplatform.tenant.service.PositionService;

/**
 * 
 * <br>
 * Class Name   : PositionController
 *
 */
@RestController
@RequestMapping("position")
public class PositionController {

	private @Autowired PositionService positionService;

	@ApiMetadata(permissionLevel = PermissionLevel.LoginRequired)
	@GetMapping("{id}")
	public Position getById(@PathVariable(value = "id") String id) {
		PositionEntity entity = positionService.findById(id);
		return BeanUtils.copy(entity, Position.class);
	}

	@ApiMetadata(permissionLevel = PermissionLevel.PermissionRequired, actionLog = true)
	@PostMapping(value = "add")
	public IdParam<String> add(@RequestBody @Validated PositionParam param) {

		PositionEntity entity = BeanUtils.copy(param, PositionEntity.class);
		positionService.addPosition(entity);

		return new IdParam<>(entity.getId());
	}

	@ApiMetadata(permissionLevel = PermissionLevel.PermissionRequired, actionLog = true)
	@PostMapping(value = "update")
	public void update(@RequestBody @Validated PositionParam param) {
		PositionEntity entity = BeanUtils.copy(param, PositionEntity.class);
		positionService.updatePosition(entity);
	}

	@ApiMetadata(permissionLevel = PermissionLevel.LoginRequired)
	@PostMapping("list")
	public Page<Position> pageQuery(@RequestBody PageQueryRequest<PositionQueryParam> param) {
		if (param.getExample() == null)
			param.setExample(new PositionQueryParam());
		Page<Position> page = positionService.pageQuery(new PageParams(param.getPageNo(), param.getPageSize()),
				param.getExample());
		return page;
	}

	@ApiMetadata(permissionLevel = PermissionLevel.PermissionRequired, actionLog = true)
	@PostMapping(value = "toggle")
	public void toggle(@RequestBody IdParam<String> param) {
		PositionEntity entity = positionService.findById(param.getId());
		entity.setEnabled(!entity.getEnabled());
		positionService.updatePosition(entity);
	}

	@ApiMetadata(permissionLevel = PermissionLevel.PermissionRequired, actionLog = true)
	@PostMapping(value = "delete")
	public void delete(@RequestBody IdParam<String> param) {
		PositionEntity entity = positionService.findById(param.getId());
		entity.setDeleted(true);
		positionService.updatePosition(entity);
	}
	
	@ApiMetadata(permissionLevel = PermissionLevel.PermissionRequired,actionLog = true)
    @GetMapping("options")
    @ResponseBody
    public List<SelectOption> options(@RequestParam(name="deptId",required=false) String deptId) {
		PositionQueryParam param = new PositionQueryParam();
		param.setEnabled(true);
    	List<PositionEntity> list = positionService.findListByParam(param);
    	return list.stream().map(e -> {
            return new SelectOption(e.getId().toString(),e.getName());
        }).collect(Collectors.toList());
    }

}
